// app.js
App({
  onLaunch(options) {
    console.log('APP-onLaunch');
    console.log(options);
    this.globalData.myDoctorId = options.query.id || 0

    // wx.showModal({
    //   title: '',
    //   content: this.globalData.myDoctorId,
    //   complete: (res) => {

    //   }
    // })
    // this.checkLogin()
  },

  checkLogin() {
    console.log('APP-checkLogin');
    var isLogin = wx.getStorageSync("isAiMinLogin");
    if (!isLogin) {
      console.log("登录");
      // 登录
      var that = this;
      wx.login({
        success: res => {
          console.log(res.code);
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var url = that.globalData.url + "user/login";
          that.wxRequest(url, {
            code: res.code,
            doctorId: that.globalData.myDoctorId,
          }, res => {
            console.log(res);
            if (res.flag) {
              if (res.data.user.birth != null) {
                res.data.user.age = this.jsGetAge(res.data.user.birth)
              }
              that.globalData.user = res.data.user;
              that.globalData.userId = res.data.user.userId;
              that.globalData.userType = res.data.user.userType;
              that.globalData.openId = res.data.user.openId;
              wx.setStorageSync('user', JSON.stringify(res.data.user))
              var doctor = res.data.doctor || ''
              wx.setStorageSync('doctor', JSON.stringify(doctor))
              if (res.data.doctor != null) {
                that.globalData.doctorId = res.data.doctor.doctorId
                that.globalData.doctor = res.data.doctor
              }
              that.globalData.openId = res.data.user.openId;
              wx.setStorageSync('isAiMinLogin', true)
              that.checkUser(res.data.user)
              if (that.CallBack) that.CallBack()
            }
          }, err => {})
        }
      })
    } else {
      console.log("有信息");
      var user = JSON.parse(wx.getStorageSync('user'))
      var doctor = JSON.parse(wx.getStorageSync('doctor'))
      if (doctor != '') {
        this.globalData.doctor = doctor
        this.globalData.doctorId = doctor.doctorId
      }
      console.log(user);
      if (user.birth != null) {
        user.age = this.jsGetAge(user.birth)
      }
      this.globalData.user = user;
      this.globalData.userId = user.userId;
      this.globalData.userType = user.userType;
      this.globalData.openId = user.openId;

      console.log(this.globalData);

      console.log(isLogin);

      this.checkUser(user)
    }
  },

  checkUser(user) {
    var that = this;
    console.log(user);
    if (user.userName == null) {
      if (that.globalData.myDoctorId != 0) {
        // 扫码进入
        wx.redirectTo({
          url: '/user/pages/userInfo/userInfo',
        })
        return
      }
      wx.showModal({
        title: '提示',
        content: '请选择您的身份',
        cancelText: '医生',
        confirmText: '用户',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/user/pages/userInfo/userInfo',
            })
          }
          if (res.cancel) {
            that.globalData.isDoctorFirst = true
            wx.navigateTo({
              url: '/doctor/pages/updateDoctorInfo/updateDoctorInfo',
            })
          }
        }
      })
    }
  },
  jsGetAge(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    var d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if (nowYear == birthYear) {
      returnAge = 0; //同年 则为0岁
    } else {
      var ageDiff = nowYear - birthYear; //年之差
      if (ageDiff > 0) {
        if (nowMonth == birthMonth) {
          var dayDiff = nowDay - birthDay; //日之差
          if (dayDiff < 0) {
            returnAge = ageDiff - 1;
          } else {
            returnAge = ageDiff;
          }
        } else {
          var monthDiff = nowMonth - birthMonth; //月之差
          if (monthDiff < 0) {
            returnAge = ageDiff - 1;
          } else {
            returnAge = ageDiff;
          }
        }
      } else {
        returnAge = -1;
      }
    }

    return returnAge;

  },

  wxRequest(url, data, succFuc, errFuc) {
    wx.request({
      url: url,
      data: data,
      method: 'POST',
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
      },
      success: res => {
        succFuc(res.data);
      },
      fail: err => {
        errFuc(err);
      }
    })
  },
  globalData: {
    url: 'http://192.168.0.107:2035/',
    // url: 'https://www.ccclkj.cn:2035/',
    path: "https://zbz-imgs.oss-cn-beijing.aliyuncs.com/",
    userId: 0,
    user: '',
    myDoctorId: 0, // 扫码绑定的医生
    doctorId: 0,
    departmentId: 1,
    userType: 0, //  0 用户   1  客服  2 医生
    doctor: '',
    isDoctorFirst: false
  }
})